#include "startscreen.h"
#include "ui_startscreen.h"
#include "helicopterform.h"
#include <QDir>

StartScreen::StartScreen(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::StartScreen)
{
    ui->setupUi(this);

    Qt::WindowFlags flags;
    flags = Qt::Dialog | Qt::WindowMinimizeButtonHint;
    setWindowFlags( flags );

    MatchRatio();
    this->setFixedSize(this->width(), this->height());
    connect(ui->enterFullscreenButton, SIGNAL(clicked()), SLOT(ToggleFullScreen()));
}

StartScreen::~StartScreen()
{
    delete ui;
}

// Match background to aspect ratio
void StartScreen::MatchRatio()
{
    // Find form's width and height
    int width = this->width();
    int height = this->height();

    // Create a QString to use with GetFilePath
    QString s;

    // Check which image to load
    int ratio = (width * 10) / height; // find a ratio to match this to

    switch (ratio) // Pick which background image to load
    {
        case 17: // 16:9 ratio
            s = GetFilePath("images/1920x1080.jpg");
            SetBackground(s);
            //SetBackground("./images/1920x1080.jpg");
            break;
        case 16: // 16:10 ratio
            s = GetFilePath("images/2560x1600.jpg");
            SetBackground(s);
            //SetBackground("./images/2560x1600.jpg");
            break;
        case 13: // 4:3 ratio
            s = GetFilePath("images/1600x1200.jpg");
            SetBackground(s);
            //SetBackground("./images/1600x1200.jpg");
            break;
        case 12: // 5:4 ratio
            s = GetFilePath("images/1280x1024.jpg");
            SetBackground(s);
            //SetBackground("./images/1280x1024.jpg");
            break;
        default: // revert to base size because resolution was not supported
            this->showNormal();
            QRect rect;
            rect.setWidth(800);
            rect.setHeight(600);
            this->setGeometry(rect);
            this->setFixedSize(rect.width(), rect.height());
            MatchRatio();
            break;
    }
    HelicopterForm::LogEvent("Resolution Set");
}

// Set the background to the window size
void StartScreen::SetBackground(QString filepath)
{
    QPalette pal = this->palette();
    pal.setBrush(QPalette::Window, QPixmap(filepath).scaled(this->size()));
    this->setPalette(pal);
    this->show();
}

QString StartScreen::GetFilePath(QString path)
{
     QDir dir(path);
     QString s, t;
     bool escape = true;

     if (dir.exists(path) == true) //if the directory exists here
        s = dir.absolutePath();    //set the path
     else
     {
         t = dir.absolutePath();
         while (dir.exists(t) == false && escape == true) // move toward the base of the directory tree
         {
            escape = dir.cdUp();
         }

         s = dir.absolutePath();
     }


     HelicopterForm::LogEvent(s);
     return s;
}

void StartScreen::startGame(bool loadGame)
{
    HelicopterForm* w = new HelicopterForm((QWidget*)this->parent());
    w->show();
    if(this->isFullScreen())
        w->ToggleFullScreen();
    this->hide();

    if(loadGame)
        w->loadSavedGame();
}
void StartScreen::on_newGameButton_clicked()
{
    this->startGame(false);
}

void StartScreen::on_loadGameButton_clicked()
{
    this->startGame(true);
}

void StartScreen::ToggleFullScreen()
{
    // if not in fullscreen
    if (this->isFullScreen() == false)
    {
        // Go fullscreen
        this->showFullScreen();

        // Update the menu item
        if (this->isFullScreen() == true)
        {
            ui->enterFullscreenButton->setText("Exit Fullscreen");
        }
    // it was in fullsreen so we exit fullscreen
    } else // exit fullscreen
    {
        this->showNormal();
        ui->enterFullscreenButton->setText("Enter Fullscreen");
    }

    MatchRatio();

    // Reposition widgets

    int formCtr = this->width() / 2;
    int newGameButtonX = formCtr - (ui->newGameButton->width() / 2),
        loadGameButtonX = formCtr - (ui->loadGameButton->width() / 2),
        enterFullscreenButtonX = formCtr - (ui->enterFullscreenButton->width() / 2),
        labelX = formCtr - (ui->label->width() / 2);

    int labelY = this->height() * 0.55,
        enterFullscreenButtonY = labelY - 70,
        loadGameButtonY = enterFullscreenButtonY - 85,
        newGameButtonY = loadGameButtonY - 105;

    ui->newGameButton->setGeometry(newGameButtonX, newGameButtonY, 200, 90);
    ui->loadGameButton->setGeometry(loadGameButtonX, loadGameButtonY, 150, 70);
    ui->enterFullscreenButton->setGeometry(enterFullscreenButtonX, enterFullscreenButtonY, 160, 40);
    ui->label->setGeometry(labelX, labelY, 520, 70);

    HelicopterForm::LogEvent("Fullscreen Toggled");
}
